<?php

namespace App\Admin\Forms;

use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;

class HospitalAppointIntervalForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $hospital_appoint_id = $input['hospital_appoint_id'];
        $hospital_id = $input['hospital_id'];

        HospitalAppointIntervalNum::query()->where('hospital_appoint_id',$hospital_appoint_id)->delete();
        foreach($input['num'] as $k=>$v){
            foreach($input['num'][$k] as $ik=>$iv){
                if($iv) {
                    $data[] = ['hospital_id'=>$hospital_id,'week' => $k, 'num' => $iv, 'interval_id' => $ik,'hospital_appoint_id'=>$hospital_appoint_id];
                }
            }
        }
        if($data) {
            try{
                DB::table('eb_hospital_appoint_interval_num')->insert($data);
            }catch (\Exception $e){
                return $this->response()->error('失败'.$e->getMessage());
            }

            //$flight = HospitalAppointIntervalNum::query()->upsert($data,['id']);
        }
        //dump($input);
        return $this->response()->success('成功');
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        \Dcat\Admin\Form::extend('IntervalWeekNum',IntervalWeekNum::class);

        $hospital_appoint_id = $this->payload['hospital_appoint_id'] ?? null;
        $hospital_id = $this->payload['hospital_id'] ?? null;

        $intervals = HospitalAppointInterval::query()->where('hospital_appoint_id',$hospital_appoint_id)->get();
        if($intervals){
            $typeText=[];
            foreach($intervals as $k=>$v){
                $typeText[$v->id]=$v->start."-".$v->end;
            }
            $weeks = [1, 2, 3, 4, 5, 6, 0];

            $row = [];
            foreach($typeText as $k=>$v){
                $row[$k]['title']=$v;
                $row[$k]['key']=$k;

                foreach($weeks as $wk=>$wv){
                    $appointsVaccine=HospitalAppointIntervalNum::query()
                    ->where('week',$wv)
                    ->where('interval_id',$k)
                    ->first();

                    $wArr[$wv]['value']=$appointsVaccine?$appointsVaccine->num:0;
                    $wArr[$wv]['key']=$wv;
                }
                $row[$k]['weeks']=$wArr;
            }

            $this->IntervalWeekNum('num')->setFields($row);
        }else{
            $this->html('请设置可预约时段后再设置号源！');
        }
        $this->hidden('hospital_appoint_id')->default($hospital_appoint_id);
        $this->hidden('hospital_id')->default($hospital_id);

    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name'  => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
